package com.ywl.stream;

import com.ywl.entity.Author;
import com.ywl.utils.AuthorFactory;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Stream流中间操作
 *
 * @author 24040
 * @name StreamDemo02
 * @date 2022-08-21 22:02
 */
public class StreamDemo02 {

    public static void main(String[] args) {
        /**
         * 打印所有姓名长度大于1的作家的姓名
         */
        List<Author> authors = AuthorFactory.getAuthors();
//        test01(authors);
//        test02(authors);
        test03(authors);
    }

    /**
     * 去重
     * @param authors
     */
    public static void test03(List<Author> authors) {
        authors.stream()
                .distinct()
                .forEach(item -> System.out.println(item));
    }

    /**
     * 中间操作map
     *
     * @param authors
     */
    private static void test02(List<Author> authors) {
        authors.stream()
                .map(author -> {
                    return author.getAge();
                })
                .map(age -> age + 10)
                .forEach(item -> System.out.println(item));
    }

    /**
     * 中间操作filter 演示
     *
     * @param authors
     */
    public static void test01(List<Author> authors) {
        authors.stream()
                .distinct()
                .filter(author -> {
                    return author.getName().length() > 1;
                })
                .forEach(author -> System.out.println(author));
    }
}
